#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1510;
const int mod = 1e9+7;

ll fac[N],ifac[N],f[N],g[N][N],invk;
int n,p,m,k;

ll qp(ll x,int t){ll res=1;for(;t;t>>=1,x=x*x%mod)if(t&1)res=res*x%mod;return res;}

void init(int n){
	for(int i = (fac[0] = 1);i <= n;i++) fac[i] = fac[i - 1] * i % mod; ifac[n] = qp(fac[n],mod-2);
	for(int i = n - 1;i >= 0;i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;
}

ll C(int x,int y){if(x < 0 || y < 0 || x < y) return 0; return fac[x] * ifac[y] % mod * ifac[x - y] % mod;}

void solve(){
	n = in,p = in,m = in,k = in;
	if(k == 0) return puts("-1"),void();
	if(m == 0 && k == 1) return puts("-1"),void();
	if(m == 0){ll ans = 0; while(p > 0) p++,p = min(p,n),p -= k,ans++;printf("%d\n",ans);return;}
	invk = qp(k,mod-2); memset(f,0,sizeof(f)); memset(g,0,sizeof(g));
	ll invv = qp(m + 1,mod - 2),ivm = qp(invv,k);
	f[0] = qp(m,k) * ivm % mod;
	for(int i = 1;i <= min(n+1,k);i++) f[i] = f[i-1] * qp(i,mod-2) % mod * qp(m,mod-2) % mod * (k - i + 1) % mod;
//	for(int i = min(n,k);i >= 0;i--) f[i] = C(k,i) * qp(m,k - i) % mod * ivm % mod;
	for(int i = 1;i < n;i++)
		for(int j = 1;j <= i;j++)
			g[i][j] = (f[i - j] * m % mod + f[i - j + 1]) * invv % mod;
	for(int i = 1;i <= n;i++) g[i][i] = (g[i][i] + mod - 1) % mod;
	for(int i = 1;i < n;i++) g[i][i + 1] = f[0] * invv % mod;
	for(int i = 1;i <= n;i++) g[i][n + 1] = mod - 1;
	for(int i = 1;i <= n;i++) g[n][i] = f[n - i]; g[n][n] = (g[n][n] + mod - 1) % mod;
	for(int i = 1;i <= n;i++){
		ll iv = qp(g[i][i],mod-2); g[i][i] = 1; g[i][n + 1] = g[i][n + 1] * iv % mod;
		if(i < n) g[i][i + 1] = g[i][i + 1] * iv % mod;
		for(int j = i + 1;j <= n;j++){
			ll t = g[j][i];
			g[j][i] = 0; g[j][i + 1] = (g[j][i + 1] - t * g[i][i + 1] % mod + mod) % mod;
			if(i < n) g[j][n + 1] = (g[j][n + 1] - t * g[i][n + 1] % mod + mod) % mod;
		}
	}
	for(int i = n;i >= 1;i--) g[i - 1][n + 1] = (g[i - 1][n + 1] + mod - g[i][n + 1] * g[i - 1][i] % mod) % mod;
	printf("%lld\n",g[p][n + 1]);
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	init(1500);
	int T = in; while(T--) solve();
	return 0;
}
